﻿using System;
using System.Collections.Generic;

namespace Restful.Net.TestHost
{
    public class Contact
    {
        public Contact()
        {
            Emails = new List<string>();
            Groups = new List<string>();
        }

        public long ContactID
        {
            get;
            set;
        }

        public Uri ContactUri
        {
            get;
            set;
        }

        public string Name
        {
            get;
            set;
        }

        public List<string> Emails
        {
            get;
            set;
        }

        public List<string> Groups
        {
            get;
            set;
        }

        public string GetETag()
        {
            HashCodeBuilder hash = new HashCodeBuilder(1, 3);
            hash.Append(ContactID);
            if (ContactUri != null)
            {
                hash.Append(ContactUri.ToString());
            }

            if (Name != null)
            {
                hash.Append(Name);
            }

            if (Emails.Count > 0)
            {
                Emails.Sort();
                Emails.ForEach(em => hash.Append(em));
            }

            if (Groups.Count > 0)
            {
                Groups.Sort();
                Groups.ForEach(g => hash.Append(g));
            }

            return hash.ToHashCode().ToString("X");
        }
    }
}
